<?php
declare (strict_types = 1);

namespace app\admin\controller\erp;

use app\admin\controller\AuthController;
use app\admin\model\system\SystemMenus;
use think\facade\Db;
use think\Request;

class Purchase extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        if ($req->isAjax()) {
            $data = [];
            $page = $req->param('page/d', 1);
            $limit = $req->param('limit/d', 15);
            $query = Db::name('erp_purchase')->alias('a')
                ->leftJoin('erp_vendors b', 'on b.id=a.vid');
            // 条件搜索
            $type_id = $req->param('type_id/d', 0);
            if ($type_id) {
                $path = VendorsTypes::where('id',$type_id)->value('path');
                $query->where('gid','in',$path);
            }
            $name = $req->param('name');
            if (!is_null($name) && strlen($name)) {
                $query->where('name','like',"%$name%");
            }
            $query->order('a.id DESC');
            $data['count'] = $query->count('a.id');
            $data['data'] = $query->field('a.*,b.name as vname')->page($page,$limit)->select()->toArray();
            $data['code'] = 0;
            $data['msg'] = '';
            foreach ($data['data'] as &$val) {
                $val['price_at'] = $val['price_at'] ? date('Y-m-d',$val['price_at']) : '--/--';
            }
            return json($data);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));
        // 显示页面
        $this->assign('sonAuth', $sonAuth);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        //
    }

    /**
     * 更新资源
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function edit(Request $req)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
